import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import mplfinance as mpf
import matplotlib.dates as mdates
import datetime as dt
import plotly.graph_objects as go
import plotly.express as px
import plotly.io as pio
from plotly.subplots import make_subplots
pio.renderers.default = "notebook"
pio.templates.default = "plotly_dark"
import gc
np.random.seed(42)
import warnings
warnings.filterwarnings("ignore")
plt.rcParams["figure.figsize"] = [12, 8]
Financial innovation is the process of creating new financial products, services, or processes. Financial innovation has come via advances in financial instruments, technology, and payment systems. Digital technology has helped to transform the financial services industry, changing how we save, borrow, invest, and pay for goods.
Some examples of financial innovation are:
Some classic examples are below:
The idea is that investors, in order to be encouraged to invest in businesses, should have protection against liability for what the managers of the business do. So, an investor should not be liable for the debts or mistake of the business. This is the idea of limited liability.
The idea was enuciated first in 1811 by New York State.
Limited liability is a type of legal structure for an organization where a corporate loss will not exceed the amount invested in a partnership or limited liability company (LLC). In other words, investors' and owners' private assets are not at risk if the company fails.
The limited liability feature is one of the biggest advantages of investing in publicly listed companies. While a shareholder can participate wholly in the growth of a company, their liability is restricted to the amount of the investment in the company, even if it subsequently goes bankrupt and has remaining debt obligations.
Instead of using a currency which is liable of changes, inflation indexed debt uses some index to tie the debt to that index.
An inflation-indexed security is a security that guarantees a return higher than the rate of inflation if it is held to maturity. Inflation-indexed securities link their capital appreciation, or coupon payments, to inflation rates.
An inflation-indexed security has its principal indexed to the Consumer Price Index (CPI), or some other nationally recognized inflation index, on a daily basis.
The Unidad de Fomento (UF) is a unit of account used in Chile. It is a non-circulating currency;the exchange rate between the UF and the Chilean peso is constantly adjusted for inflation so that the value of the Unidad de Fomento remains almost constant on a daily basis during low inflation.
It has become the preferred and predominant measure to determine the cost of real estate, values of housing and any secured loan, either private or of the Chilean government. Individual payments are made in Chilean pesos (the country's legal tender), according to the daily value of the UF.
This is an innovation to deal with inflation.
Forecasting is the process of making predictions based on past and present data. Later these can be compared (resolved) against what happens. For example, a company might estimate their revenue in the next year, then compare it against the actual results. Prediction is a similar but more general term.
Something that happended on past is not the represenative of what will happen in future.
Random walk theory suggests that changes in stock prices have the same distribution and are independent of each other. Therefore, it assumes the past movement or trend of a stock price or market cannot be used to predict its future movement. In short, random walk theory proclaims that stocks take a random and unpredictable path that makes all methods of predicting stock prices futile in the long run.
We have
$$ x_{t} = x_{t-1}+\epsilon_t $$where $\epsilon$ is a noise and is completely unforcastable.
apple = pd.read_csv("Data/AAPL.csv", parse_dates=["Date"], index_col="Date")
google = pd.read_csv("Data/GOOG.csv", parse_dates=["Date"], index_col="Date")
apple.head()
apple = apple[apple.index >= pd.to_datetime("2022-01-01", format="%Y-%m-%d", utc=True)]
apple = apple[["Close"]]
apple.head()
px.line(apple, x=apple.index, y="Close", title="Apple Stock Price")
sigma = apple["Close"].std()
print("Standard Deviation: ", sigma)
sigma = 3
walk_prices = []
start_price = apple["Close"].iloc[-1]
cur_price = start_price
walk_prices.append(cur_price)
for i in range(1, 200):
cur_price = cur_price + np.random.normal(0, sigma)
walk_prices.append(cur_price)
walk_prices = np.array(walk_prices)
fig = px.line(x=range(0, 200), y=walk_prices, title="Apple Stock Price (Random Walk)")
fig.update_xaxes(title_text="Days")
fig.update_yaxes(title_text="Price")
fig.show()
Let's merge both the plots on one graph.
# walk_prices = np.array(walk_prices)
# prices = np.concatenate((apple["Close"].values, new_prices))
# prices = pd.DataFrame(prices, columns=["Close"])
# prices.index = apple.index.append(pd.date_range(start=apple.index[-1], periods=200, freq="D"))
# prices.head()
fig = px.line(
y=apple["Close"], x=np.arange(0, len(apple["Close"])), title="Apple Stock Price"
)
fig.add_scatter(
y=walk_prices,
x=np.arange(len(apple["Close"]), len(apple["Close"]) + len(walk_prices)),
mode="lines",
name="Random Walk",
)
Next, we'll repeat the simulation a number of times and plot the results.
def simulate_one(days=200, sigma=3):
walk_prices = []
start_price = apple["Close"].iloc[-1]
cur_price = start_price
walk_prices.append(cur_price)
for i in range(1, 200):
cur_price = cur_price + np.random.normal(0, sigma)
walk_prices.append(cur_price)
walk_prices = np.array(walk_prices)
return walk_prices
# simulate 100 random walks and plot them
# We'll also plot the mean of the random walks
walk_prices = simulate_one()
sum_walk_price = walk_prices
fig = px.line(y=walk_prices, x=np.arange(0, 200))
for i in range(100):
walk_prices = simulate_one()
sum_walk_price += walk_prices
fig.add_scatter(y=walk_prices, x=np.arange(0, 200), mode="lines", name=f"S_{i+1}")
fig.add_scatter(
y=sum_walk_price / 100,
x=np.arange(0, 200),
mode="lines",
name=f"Avereged",
line=dict(color="royalblue", width=4),
)
fig.show()
A statistical model is autoregressive if it predicts future values based on past values. For example, an autoregressive model might seek to predict a stock's future prices based on its past performance.
Autoregressive models operate under the premise that past values have an effect on current values, which is an opposite view to that of random walk theory. In other words, autoregressive models assume that past values can be used to predict future values.
In this case, we have
$$ x_t = x_0 + \rho(x_{t-1}-x_0) + \epsilon _t $$$x_0$ is the initial position. If $\rho =1$ this gives the random walk.
This is a modified version of random walk. The middle term means that as the position is away from the mean position, it will try to move to the mean position.
rho = 0.1
ar_prices = []
start_price = apple["Close"].iloc[-1]
cur_price = start_price
ar_prices.append(cur_price)
for i in range(1, 200):
cur_price = (
start_price + rho * (cur_price - start_price) + np.random.normal(0, sigma)
)
ar_prices.append(cur_price)
fig = px.line(
y=apple["Close"], x=np.arange(0, len(apple["Close"])), title="Apple Stock Price"
)
fig.add_scatter(
y=ar_prices,
x=np.arange(len(apple["Close"]), len(apple["Close"]) + len(ar_prices)),
mode="lines",
name=f"AR(1) for rho={rho}",
)
rho = 0.5
ar_prices = []
start_price = apple["Close"].iloc[-1]
cur_price = start_price
ar_prices.append(cur_price)
for i in range(1, 200):
cur_price = (
start_price + rho * (cur_price - start_price) + np.random.normal(0, sigma)
)
ar_prices.append(cur_price)
fig = px.line(
y=apple["Close"], x=np.arange(0, len(apple["Close"])), title="Apple Stock Price"
)
fig.add_scatter(
y=ar_prices,
x=np.arange(len(apple["Close"]), len(apple["Close"]) + len(ar_prices)),
mode="lines",
name=f"AR(1) for rho={rho}",
)
rho = 0.9
ar_prices = []
start_price = apple["Close"].iloc[-1]
cur_price = start_price
ar_prices.append(cur_price)
for i in range(1, 200):
cur_price = (
start_price + rho * (cur_price - start_price) + np.random.normal(0, sigma)
)
ar_prices.append(cur_price)
fig = px.line(
y=apple["Close"], x=np.arange(0, len(apple["Close"])), title="Apple Stock Price"
)
fig.add_scatter(
y=ar_prices,
x=np.arange(len(apple["Close"]), len(apple["Close"]) + len(ar_prices)),
mode="lines",
name=f"AR(1) for rho={rho}",
)
rho = 1.0
ar_prices = []
start_price = apple["Close"].iloc[-1]
cur_price = start_price
ar_prices.append(cur_price)
for i in range(1, 200):
cur_price = (
start_price + rho * (cur_price - start_price) + np.random.normal(0, sigma)
)
ar_prices.append(cur_price)
fig = px.line(
y=apple["Close"], x=np.arange(0, len(apple["Close"])), title="Apple Stock Price"
)
fig.add_scatter(
y=ar_prices,
x=np.arange(len(apple["Close"]), len(apple["Close"]) + len(ar_prices)),
mode="lines",
name=f"AR(1) for rho={rho}",
)
Same as before, we'll make a plot with multiple simulations. We'll be using $\rho = 0.9$
def simulate_one(days=200, sigma=3, rho=0.9):
ar_prices = []
start_price = apple["Close"].iloc[-1]
cur_price = start_price
ar_prices.append(cur_price)
for i in range(1, 200):
cur_price = (
start_price + rho * (cur_price - start_price) + np.random.normal(0, sigma)
)
ar_prices.append(cur_price)
ar_prices = np.array(ar_prices)
return ar_prices
ar_prices = simulate_one()
sum_ar_price = ar_prices
fig = px.line(y=ar_prices, x=np.arange(0, 200))
for i in range(100):
ar_prices = simulate_one()
sum_ar_price += ar_prices
fig.add_scatter(y=ar_prices, x=np.arange(0, 200), mode="lines", name=f"S_{i+1}")
fig.add_scatter(
y=sum_ar_price / 100,
x=np.arange(0, 200),
mode="lines",
name=f"Avereged",
line=dict(color="royalblue", width=4),
)
fig.show()
The efficient market hypothesis (EMH), alternatively known as the efficient market theory, is a hypothesis that states that share prices reflect all information.
According to the EMH, stocks always trade at their fair value on exchanges, making it impossible for investors to purchase undervalued stocks or sell stocks for inflated prices. Therefore, it should be impossible to outperform the overall market through expert stock selection or market timing, and the only way an investor can obtain higher returns is by purchasing riskier investments.
Weak Form: prices incorporate information about past prices
Semi Strong Form: all publicly information is already incorporated in the market prices
Strong Form: all information including inside information held by the companies is already incorporated in the stock prices, prices because it leaks out.
The hypothesis is only a "half truth".
Discounted present value is a concept in economics and finance that refers to a method of measuring the value of payments or utility that will be received in the future. Most people would agree that receiving $1,000 today is better than receiving $1,000 in a year, because $1,000 today can be used for consumption or investment.
By Gordon model, if the earnings by a stock is due to dividends and the dividends grow with factor g, we have:
$$ P = \frac{E}{r-g}\\ \frac{P}{E} = \frac{1}{r-g} $$$E$ can be seen as the value of the next year's dividends.
This means that if the P/E ratio is higher it would either have to be because it's low risk as measured by beta, so we willing to pay more for it because it's low risk. Or would have to be that people have reason to think that they're earning path the gross rate g is high.
Behavioral finance, a subfield of behavioral economics, proposes that psychological influences and biases affect the financial behaviors of investors and financial practitioners. Moreover, influences and biases can be the source for the explanation of all types of market anomalies and specifically market anomalies in the stock market, such as severe rises or falls in stock price.
In economics, utility is a term used to determine the worth or value of a good or service. More specifically, utility is the total satisfaction or benefit derived from consuming a good or service. Economic theories based on rational choice usually assume that consumers will strive to maximize their utility.
People are rational and are trying to maximize their utility which is an measure of happiness.
Prospect theory is is a experimentally based set of knowledge about mistakes that people make. This is based on research by Kahneman and Tversky of people making decisions under uncertainty and is a modification of the expected utility theory. The theory is based on some real life facts such as:
Prospect theory assumes that losses and gains are valued differently, and thus individuals make decisions based on perceived gains instead of perceived losses. Also known as the "loss-aversion" theory, the general concept is that if two choices are put before an individual, both equal, with one presented in terms of potential gains and the other in terms of possible losses, the former option will be chosen.
The main assumption of the prospect theory is that people are not very rational; which is in contrast with the traditional economic models that people are very sensible and calculating.
Efficient market hypothesis and behavioral finance are two main revolution of past 50-60 years. These two revolutions are kind of incompatible views of the world. But they both offer insights, they're both exciting.
Tversky and Kahneman proposed that losses cause a greater emotional impact on an individual than does an equivalent amount of gain, so given choices presented two ways—with both offering the same result—an individual will pick the option offering perceived gains.
For example, assume that the end result of receiving 25. One option is being given 25 outright. The other option is being given 50 and then having to give back 25. The utility of the 25 is exactly the same in both options. However, individuals are most likely to choose to receive straight cash because a single gain is generally observed as more favorable than initially having more cash and then suffering a loss.
Example: Let's say I toss a coin. If it turns head, I'll give you 200 and if it turns tail you'll give me 100. So, you have an expected return of 50. By expected utility theory, you should take the bet. However, people don't usually take the bet.
This can be understood by the prospect thoery. People don't take the bet because the gaining 200 gives a little happiness but losing 100 gives a lot of unhappiness. This is called the loss aversion namely, people are more afraid of loosing. ("You are always worried about little losses today.")

Anchoring refers to a tendency in ambiguous situations to allow one's decisions to be affected by some anchor. it's the same way I think with stock prices, that stock prices are anchored to past values. Nobody knows what this company is worth but it was worth something yesterday.